<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="所属年月" prop="belongMonth">
        <el-date-picker
          v-model="queryParams.belongMonth"
          value-format="YYYY-MM"
          type="monthrange"
          placeholder="选择所属年月"
          clearable
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="部门" prop="deptId">
        <el-select v-model="queryParams.deptId" placeholder="请选择部门" clearable class="!w-240px" multiple collapse-tags>
          <el-option
            v-for="dict in deptArr"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="指标点" prop="performancePoint">
        <el-select
          v-model="queryParams.performancePoint"
          placeholder="请选择指标点"
          clearable  multiple collapse-tags
          class="!w-240px"
        >
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.CRM_PERFORMANCE_POINT)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table
      row-key="id"
      v-loading="loading"
      :data="list"
      :stripe="true"
      :show-overflow-tooltip="true"
    >
      <el-table-column label="所属年月" align="center" prop="belongMonth"/>
      <el-table-column label="部门" align="center" prop="deptId">
        <template #default="scope">
          <arr-dict-tag :dict-arr="deptArr" :value="scope.row.deptId" />
        </template>
      </el-table-column>
      <el-table-column label="指标点" align="center" prop="performancePoint">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.CRM_PERFORMANCE_POINT" :value="scope.row.performancePoint" />
        </template>
      </el-table-column>
      <el-table-column label="目标值" align="center" prop="targetValue" />
      <el-table-column label="完成值" align="center" prop="finishValue" />
      <el-table-column label="完成率" align="center" prop="finishRate" />

      <!--      <el-table-column label="操作" align="center" min-width="120px">-->
      <!--        <template #default="scope">-->
      <!--          <el-button-->
      <!--            link-->
      <!--            type="primary"-->
      <!--            @click="openForm('update', scope.row.id)"-->
      <!--            v-hasPermi="['crm:performance:config:update']"-->
      <!--          >-->
      <!--            编辑-->
      <!--          </el-button>-->
      <!--          <el-button-->
      <!--            link-->
      <!--            type="danger"-->
      <!--            @click="handleDelete(scope.row.id)"-->
      <!--            v-hasPermi="['crm:performance:config:delete']"-->
      <!--          >-->
      <!--            删除-->
      <!--          </el-button>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

</template>

<script setup lang="ts">
import { getStrDictOptions, DICT_TYPE } from '@/utils/dict'
import {
  PerformanceConfigApi,
  PerformanceResult
} from '@/api/crm/performance/config'
import * as DeptApi from '@/api/system/dept'

/** CRM_业绩配置 列表 */
defineOptions({ name: 'PerformanceResult' })
let deptArr = reactive([])
const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<PerformanceResult[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  belongMonth: undefined,
  deptId: undefined,
  performancePoint: undefined
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await PerformanceConfigApi.getPerformanceResultPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}


/** 初始化 **/
onMounted(async () => {
  const deptList = await DeptApi.getDeptList({})
  let deptArrTemp = []
  for (const element of deptList) {
    deptArrTemp.push({ label: element.name, value: element.id + '' })
  }
  deptArr = deptArrTemp
  console.debug('deptArr', deptArr)
  getList()
})
</script>
